<?php

    if ((!isset($_POST['install_step1']) && !isset($_POST['install_step2']))
            && file_exists("./config.php"))
        header("Location: ./admin");

    include("./global_conf.php");
    require_once($site->libdir."/installib.php");

    if (isset ($_POST['install_step1'])) {
        foreach ($_POST as $val) {
            if (is_null($val) || empty($val)) {
                $err="Error: missing field(s)";
                unset($_GET['step']);
                break;
            }
        }
        if (!isset($err)) {
            create_db_user($_POST);
            $str=$_POST['url'];
            if ($str[strlen($str)-1] != '/')
                $_POST['url'].="/";
            write_config_php($site->homedir, $_POST);
            if (!isset($_GET['step']) || intval($_GET['step']) != 2) {
                header("Location: install.php?step=2");
                die;
            }
        }
    }

    else if (isset ($_POST['install_step2'])) {
        foreach ($_POST as $val) {
            if (is_null($val) || empty($val)) {
                $err="Error: missing field(s)";
                break;
            }
        }
        if (strcmp($_POST['admin_passwd'], $_POST['admin_passwd_repeat']))
                $err="Error: passwords don't match";
        if (!isset($err)) {
            require_once($site->classdir."/DBmanager.php");
            
            create_core_system($_POST['admin_usr'], $_POST['admin_passwd']);
            header("Location: ./admin/login.php");
            die;
        }
    }
    include($site->includedir."/install_header.inc.php");
?>
<h1 id="titleh1">Qms installation</h1>
<div id="page">
    <div id="content">
        <div id="intro">
            Before you start working on Qms, you need to provide some information
        </div>
        <?php
            if (!isset($_GET['step']) || intval($_GET['step']) == 1) {
echo <<<EOT
        <form action="?step=2" method="post">
            <fieldset>
                <legend>Database informations</legend>
                <div class="formsection">
                    <h4>Please provide a valid user with all grants on your database</h4>
                    <label>Username</label>
                    <input class="forminput" type="text" name="dbusr" value="root" />
                    <label>Password</label>
                    <input class="forminput" type="password" name="dbpasswd" value="" />
                </div>
                <div class="formsection">
                    <h4>Enter data for the NEW database</h4>
                    <label>Username</label>
                    <input class="forminput" type="text" name="newusr" value="qms" />
                    <label>Password</label>
                    <input class="forminput" type="password" name="newpasswd" value="" />
                    <label>DB name</label>
                    <input class="forminput" type="text" name="newdb" value="qmsdb" />
                </div>
                <div class="formsection">
                    <h4>Site specific data</h4>
                    <label>Url</label>
                    <input class="forminput" type="text" name="url" value="http://$_SERVER[SERVER_NAME]" />
                </div>
                <input class="forminput" type="submit" name="install_step1" value="Create" />
            </fieldset>
        </form>
EOT;
            }
            else if (isset($_GET['step']) && intval($_GET['step']) == 2) {
echo <<<EOT
        <form action="?step=2" method="post">
            <fieldset>
                <legend>User informations</legend>
                <div class="formsection">
                    <h4>Sign up the administrator account</h4>
                    <label>Username</label>
                    <input class="forminput" type="text" name="admin_usr" value="" />
                    <label>Password</label>
                    <input class="forminput" type="password" name="admin_passwd" value="" />
                    <label>Repeat password</label>
                    <input class="forminput" type="password" name="admin_passwd_repeat" value="" />
                </div>
                <input class="forminput" type="submit" name="install_step2" value="Create" />
            </fieldset>
        </form>
EOT;
            }
        ?>
    </div>
</div>
<?php
    if (isset($err))
        echo "<div style=\"background-color: red; width: 400px; margin: auto\">$err</div>";
    include($site->includedir."/install_footer.inc.php")
?>